// TypeScript file
class GameOverView extends egret.Sprite
{
    private bg: egret.Bitmap;
    private container: egret.Sprite;
    private score: SpecialNumber;
    private thisNum: number = 0;
    private num: number = 0;
    private t1Num: number = 0;
    private t2Num: number = 0;
    private t3Num: number = 0;
    private t1: SpecialNumber;
    private t2: SpecialNumber;
    private t3: SpecialNumber;
    private huliW: number = 0;
    private langW: number = 0;
    private bianfuW: number = 0;
    private tt: egret.TextField;
    private ttNum: number = 0;
    private tNum: number = 0;

    private spGengduo: egret.Sprite;
    private spZaiLai: egret.Sprite;
    private spFenXiang: egret.Sprite;
    private sp: egret.Sprite;
    private boo1: boolean = false;
    private boo2: boolean = false;
    private boo3: boolean = false;

    constructor()
    {
        super();
        this.initView();
    }

    private initView(): void
    {
        this.container = new egret.Sprite();
        this.addChild(this.container);

        this.bg = ResourceUtils.createBitmapByName("overBg_png");
        this.container.addChild(this.bg);

        this.initEnemy(GameData.curScene);

        this.score = new SpecialNumber("number-0");
        this.container.addChild(this.score);
        this.score.x = Const.SENT_WIDTH / 2 - this.score.width / 2;
        this.score.y = Const.SENT_HEIGHT / 6;
        egret.Ticker.getInstance().register(this.showScore, this);

        this.tt = new egret.TextField();
        this.addChild(this.tt);

        let b: number;
        if (GameData.isWin)
        {
           b = Math.floor(Math.random() * 15 + 80); 
        }
        else
        {
            b = Math.floor(Math.random() * 40 + 40);
        }

        this.ttNum = b;
        this.tt.text = "超越了丛林中" + 0 + "%的小伙伴";
        this.tt.textColor = 0x000000;
        this.tt.bold = true;
        this.tt.size = 32;
        this.tt.x = this.container.width / 2 - this.tt.width / 2 - 10;
        this.tt.y = 505;

        this.spGengduo = new egret.Sprite();
        this.container.addChild(this.spGengduo);
        let gengduo: egret.Bitmap = ResourceUtils.createBitmapByName("btngengduoyouxi_png");
        this.spGengduo.addChild(gengduo);
        this.spGengduo.touchEnabled = true;
        this.spGengduo.addEventListener(egret.TouchEvent.TOUCH_TAP, this.toGengDuoView, this);

        this.spZaiLai = new egret.Sprite();
        this.container.addChild(this.spZaiLai);
        let zailai: egret.Bitmap = ResourceUtils.createBitmapByName("btnzailaiyici_png");
        this.spZaiLai.addChild(zailai);
        this.spZaiLai.touchEnabled = true;
        this.spZaiLai.addEventListener(egret.TouchEvent.TOUCH_TAP, this.toGameStartView, this);

        this.spFenXiang = new egret.Sprite();
        this.container.addChild(this.spFenXiang);
        let fenxiang: egret.Bitmap = ResourceUtils.createBitmapByName("btnfenxiang_png");
        this.spFenXiang.addChild(fenxiang);
        this.spFenXiang.touchEnabled = true;
        this.spFenXiang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.toShareView, this);

        this.spGengduo.x = 12;
        this.spGengduo.y = this.spFenXiang.y = this.spZaiLai.y = 600;
        this.spZaiLai.x = 170;
        this.spFenXiang.x = 330;

        if (egret.Capabilities.runtimeType == egret.RuntimeType.NATIVE)
        {
            this.container.removeChild(this.spGengduo);
            this.container.removeChild(this.spFenXiang);
        }

        this.sp = new egret.Sprite();
        this.addChild(this.sp);
        let bg: egret.Bitmap = ResourceUtils.createBitmapByName("share_png");
        bg.width = Const.SENT_WIDTH;
        bg.height = Const.SENT_HEIGHT;
        this.sp.addChild(bg);
        this.sp.visible = false;
        this.touchEnabled = true;
        this.sp.addEventListener(egret.TouchEvent.TOUCH_TAP, this.touchShare, this);

        this.container.scaleX = this.container.scaleY = 0.9;
        this.container.x = Const.SENT_WIDTH / 2 - this.container.width / 2 + 30;
        this.container.y = Const.SENT_HEIGHT / 2 - this.container.height / 2 + 30;

    }

    private initEnemy(curScene: number): void
    {
        if (curScene == 1 || curScene == 2)
        {
            this.boo1 = true;
            
            let lang: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("lang");
            this.container.addChild(lang);
            lang.goToPlay("run");
            lang.y = 340;
            lang.x = 140 + lang.width;
            this.langW = lang.x;
            
            this.t1 = new SpecialNumber("number-");
            this.container.addChild(this.t1);
            
            this.t1.setValue(0+"");
            this.t1.x = lang.x + this.t1.width / 2;
            this.t1.y = lang.y + lang.height * 0.6 - 20;
            
        }
        else if (curScene == 3 || curScene == 4)
        {
            this.boo1 = true;
            this.boo2 = true;
            let lang: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("lang");
            let huli: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("huli");
            this.container.addChild(lang);
            this.container.addChild(huli);
            lang.goToPlay("run");
            huli.goToPlay("run");

            huli.y = 320;
            lang.y = 340;
            lang.x = (220 + lang.width) / 2;
            huli.x = lang.x + lang.width + 60;

            this.langW = lang.x;
            this.huliW = huli.x;

            this.t1 = new SpecialNumber("number-");
            this.container.addChild(this.t1);
            this.t1.setValue(0+"");
            this.t1.x = lang.x - this.t1.width / 2;
            this.t1.y = lang.y + lang.height * 0.6 - 20;

            this.t2 = new SpecialNumber("number-");
            this.container.addChild(this.t2);
            this.t2.x = huli.x - this.t2.width / 2;
            this.t2.y = huli.y + lang.height + 0.6;
        }
        else if (curScene == 5 || curScene == 6)
        {
            this.boo1 = true;
            this.boo2 = true;
            this.boo3 = true;
            let lang: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("lang");
            let huli: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("huli");
            let bianfu: StarlingSwfMovieClip = StarlingSwfFactory.getInstance().makeMc("bianfu");
            this.container.addChild(lang);
            this.container.addChild(huli);
            this.container.addChild(bianfu);
            lang.goToPlay("run");
            huli.goToPlay("run");
            bianfu.goToPlay("run");

            huli.y = 320;
            bianfu.y = 335;
            lang.y = 340;

            lang.x = 90;
            huli.x = lang.x + lang.width + 60;
            bianfu.x = huli.x + huli.width + 70;

            this.huliW = huli.x;
            this.langW = lang.x;
            this.bianfuW = bianfu.x;

            this.t1 = new SpecialNumber("number-");
            this.t2 = new SpecialNumber("number-");
            this.t3 = new SpecialNumber("number-");
            this.container.addChild(this.t1);
            this.container.addChild(this.t2);
            this.container.addChild(this.t3);
            this.t2.setValue(0+"");
            this.t3.setValue(0+"");
            this.t1.setValue(0+"");

            this.t1.x = lang.x - this.t1.width / 2;
            this.t1.y = lang.y + lang.height * 0.6 - 20;

            this.t2.x = huli.x - this.t2.width / 2;
            this.t2.y = huli.y + lang.height * 0.6;

            this.t3.x = bianfu.x - this.t3.width / 2;
            this.t3.y = bianfu.y + lang.height * 0.6 - 15;
        }
        

    }

    private showScore(evt: egret.Event): void
    {
        ++this.num;
        if (this.thisNum < GameData.score)
        {
            this.thisNum += this.num;
            if (this.thisNum > GameData.score)
            {
                this.thisNum = GameData.score;
            }
            this.score.setValue(this.thisNum + "");
            this.score.x = Const.SENT_WIDTH / 2 - this.score.width / 2;
        }

        if (this.boo1)
        {
            if (this.t1Num < GameData.langNum)
            {
                this.t1Num += 3;
                if (this.t1Num > GameData.langNum)
                {
                    this.t1Num = GameData.langNum;
                }

                this.t1.setValue(this.t1Num + "");
                this.t1.x = this.langW + this.t1.width / 2;
            }
        }

        if (this.boo2)
        {
            if (this.t2Num < GameData.huliNum)
            {
                this.t2Num += 3;
                if (this.t2Num > GameData.huliNum)
                {
                    this.t2Num = GameData.huliNum;
                }
                this.t2.setValue(this.t2Num + "");
                this.t2.x = this.huliW - this.t2.width / 2;
            }
        }

        if (this.boo3)
        {
            if (this.t3Num < GameData.bianfuNum)
            {
                this.t3Num += 3;
                if (this.t3Num > GameData.bianfuNum)
                {
                    this.t3Num = GameData.bianfuNum;
                }
                this.t3.setValue(this.t3 + "");
                this.t3.x = this.bianfuW - this.t3.width / 2;
            }
        }

        if (this.tNum < this.ttNum)
        {
            this.tNum += 2;
            if (this.tNum > this.ttNum)
            {
                this.tNum = this.ttNum;
            }

            this.tt.text = "超越了丛林中" + this.tNum + "%的小伙伴";
            this.tt.x = this.container.width / 2 - this.tt.width / 2 - 10;
        }
        
    }

    private toGengDuoView(): void
    {
        this.spGengduo.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.toGengDuoView, this);
    }

    private toGameStartView(): void
    {
        GameData.curScene = 1;
        GameData.score = 0;
        GameData.langNum = 0;
        GameData.huliNum = 0;
        GameData.bianfuNum = 0;
        GameData.isPause = true;
        GameData.count = 0;
        GameData.profectNum = 0;
        GameData.stopCreateEnemy = 0;
        GameData.redGirlDistance = 0;
        GameData.blood = 10;
        GameData.maxBlood = 10;

        this.spGengduo.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.toGengDuoView, this);
        this.spFenXiang.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.toShareView, this);
        this.spZaiLai.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.toGameStartView, this);

        this.removeChildren();
        if (this.parent)
        {
            this.parent.removeChild(this);

        }

        GameSceneView.gameScene.start();
    }

    private toShareView(): void
    {

    }

    private touchShare(): void
    {

    }
}